import { memberList, memberOtherList, findByMobile, memberSummary } from '../../../../api/member'
import { getInfo } from '../../../../api/mch'
const util = require('../../../../utils/util.js')
let app = getApp();
Page({
  data: {
    currentTab: 0,
    mch: {},
    pageNum: 1,
    swiperHeight: '600rpx',
    customerList: [],
    customerOtherList: [],
    searchList: [],
    vipType: '',
    arrType: [
      { number: 0, value: "全部客户", attrs: "count" },
      { number: 0, value: "成交客户", attrs: "effective" },
      { number: 0, value: "潜在客户", attrs: "potential" },
      { number: 0, value: "主理人", attrs: "agent" },
      { number: 0, value: "会员客户", attrs: "vipMember" },
      { number: 0, value: "零售客户", attrs: "retail" }],
    memberSummaryData: {},
    type: '',
    mobile: '',
    content: [
      // "该用户已加入其他团队。",
      // "该用户已注册，等待您的美丽支持！",
      // "该用户未注册，等待您的美丽支持。",
      "该客户已绑定！",
      "该客户暂未绑定。",
      "该客户暂未注册。",
    ],
    hidden: false,
    define_avatar: "https://rzico-weex.oss-cn-hangzhou.aliyuncs.com/45082/avatar.png",
  },
  loadMch() {
    var _this = this;
    getInfo(app.globalData.mchId).then(res => {
      _this.data.mch = res.data;
      _this.setData({
        mch: _this.data.mch
      })
    });
  },
  loadMemberList() {
    this.loadMch()
    if (this.data.pageNum == 1) {
      this.data.customerList = []
    }
    memberList({
      // status: this.data.status,
      pageNum: this.data.pageNum,
      pageSize: 20,
    }).then(res => {
      this.data.swiperHeight = 'height:' + ((res.data.data.length * 170) + 140) + 'rpx'
      res.data.data.forEach(item => {
        if (!util.isNull(item.avatar)) {
          item.avatar = util.thumbnail(item.avatar, 100, 100)
        } else {
          // item.avatar = this.data.mch.logo
          item.avatar = util.thumbnail(this.data.define_avatar, 100, 100)
        }
        if (item.nearBuyDate != null) {
          item.nearBuyDate = util.datetimeFormat(item.nearBuyDate)
        }
      })
      res.data.data.forEach(item => {
        this.data.customerList.push(item)
      })
      this.setData({
        customerList: this.data.customerList,
        swiperHeight: this.data.swiperHeight
      })
    })
  },
  loadMemberOtherList() {
    this.loadMch()
    if (this.data.pageNum == 1) {
      this.data.customerOtherList = []
    }
    memberOtherList({
      // status: this.data.status,
      pageNum: this.data.pageNum,
      pageSize: 20,
    }).then(res => {
      this.data.swiperHeight = 'height:' + ((res.data.data.length * 170) + 140) + 'rpx'
      res.data.data.forEach(item => {
        if (!util.isNull(item.avatar)) {
          item.avatar = util.thumbnail(item.avatar, 100, 100)
        } else {
          // item.avatar = this.data.mch.logo
          item.avatar = util.thumbnail(this.data.define_avatar, 100, 100)
        }
        if (item.nearBuyDate != null) {
          item.nearBuyDate = util.datetimeFormat(item.nearBuyDate)
        }
      })
      res.data.data.forEach(item => {
        this.data.customerOtherList.push(item)
      })
      this.setData({
        customerOtherList: this.data.customerOtherList,
        swiperHeight: this.data.swiperHeight
      })
    })
  },
  reachBottom() {
    this.data.pageNum += 1
    if (this.data.currTable == 0) {
      this.loadMemberList()
    } else {
      this.loadMemberList()
    }
  },
  bindChange: function (e) {
    var that = this;
    let currTable = e.detail.current
    this.data.pageNum = 1
    if (currTable == 0) {
      this.data.customerList = []
      that.loadMemberList()
      that.setData({
        customerList: this.data.customerList,
        currentTab: currTable
      })
    } else {
      this.data.customerOtherList = []
      that.loadMemberOtherList()
      that.setData({
        customerOtherList: this.data.customerOtherList,
        currentTab: currTable
      })
    }
  },
  swichNav: function (e) {
    var that = this;
    let cerrTable = e.currentTarget.dataset.current
    this.data.pageNum = 1
    if (this.data.currentTab === cerrTable) {
      return false;
    } else {
      that.setData({
        currentTab: cerrTable
      })
    }
  },
  memberOrder(data) {
    const { id, name } = data.currentTarget.dataset;
    // console.log("选择客户",data.currentTarget.dataset);
    wx.navigateTo({
      url: `/pages/functions/jumpPage/popularize/performance/performance?id=${id}&name=${name}`
    })

    // if (this.data.vipType > 1) {
    //   wx.navigateTo({
    //     url: `/pages/functions/jumpPage/popularize/memberInfo?id=${id}`
    //   })
    // } else {
    //   wx.navigateTo({
    //     url: `pages/functions/jumpPage/popularize/performance/performance?id=${id}`
    //   })
    // }

  },
  onLoad: function (options) {
    this.data.vipType = options.vipType
    if (this.data.vipType == '') {
      this.data.vipType = app.globalData.userVipType
    }
    this.setData({
      vipType: this.data.vipType,
      hidden: false
    })
    this.loadMemberList()
    this.loadMemberSummary()
  },
  bindfocus() {
    this.setData({
      hidden: true,
      searchList: this.data.searchList
    })
  },

  del() {
    this.data.mobile = ''
    this.setData({
      mobile: this.data.mobile,
    })
  },

  closeClick() {
    this.data.mobile = ''
    this.data.searchList = []
    this.setData({
      hidden: false,
      mobile: '',
      searchList: this.data.searchList,
    })
  },
  bindKeyInput: function (e) {
    if (e.detail.value != '') {
      this.data.mobile = e.detail.value
      this.setData({
        mobile: this.data.mobile
      })
      if (this.data.mobile.length == 11) {
        this.queryMember()
      }
    }
  },
  bindconfirm(e) {
    this.setData({
      mobile: e.detail.value
    })
    this.queryMember()
  },
  queryMember() {
    let mobile = this.data.mobile
    if (util.isNull(mobile)) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'error', 
        duration: 2000
      })
      return;
    }
    this.data.searchList = []
    this.setData({
      searchList: this.data.searchList
    })
    findByMobile({ mobile: mobile }).then(res => {
      let list = {}
      this.data.searchList = []
      this.setData({
        hidden: true
      })
      if (res.type == 'success') {
        list = {
          avatar: res.data.avatar,
          id: res.data.member.id,
          name: res.data.member.name,
          mobile: res.data.member.mobile,
          createDate: util.datetimeFormat(res.data.member.createDate),
          vipName: res.data.member.vipName,
          promoterName: res.data.promoterName,
          agentName: res.data.agentName,
          amount: res.data.member.amount,
        }
        if (list.avatar == null) {
          list.avatar = this.data.define_avatar
        }
        if (res.data.member.amount > 0) {
          list['content'] = this.data.content[0]
        } else
          list['content'] = this.data.content[1];
      }
      this.data.searchList.push(list)
      this.setData({
        searchList: this.data.searchList,
      })
    }).catch(error => {
      if (error.type == 'error') {
        this.data.type = error.type
        this.setData({
          type: this.data.type
        })
      }
    })
  },
  loadMemberSummary() {
    memberSummary({
      status: this.data.status,
      pageNum: this.data.pageNum,
      pageSize: 20,
    }).then(res => {
      if (res.type = "success") {
        this.data.memberSummaryData = res.data
        // let arr = Object.values(res.data)
        // let count = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
        let count = Number(res.data.effective) + Number(res.data.potential)
        this.data.arrType.forEach(item => {
          for (var propertyName in res.data) {
            if (item.attrs == propertyName) {
              item.number = res.data[propertyName]
            }
          }
          if (item.attrs == "count") {
            item.number = count
          }
        })
        this.setData({
          arrType: this.data.arrType
        })

      }
    })
  },
  toCall(e) {
    let mobile = e.currentTarget.dataset.mobile
    if (!util.isNull(mobile)) {
      wx.makePhoneCall({
        phoneNumber: mobile
      })
    }
  },
})
